package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.BonusDetailService;
import com.qigao.mall.commons.dto.BonusCondDto;
import com.qigao.mall.commons.dto.BonusDetailResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.UserLevelEnum;
import com.qigao.mall.commons.enums.UserTypeEnum;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.jdbc.mapper.TbBonusDetailMapper;
import com.qigao.mall.jdbc.model.TbBonusDetail;
import com.qigao.mall.jdbc.model.TbSettleDetail;
import org.apache.xmlbeans.UserType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BonusDetailServiceImpl implements BonusDetailService {
    @Resource
    private TbBonusDetailMapper tbBonusDetailMapper;
    @Override
    public void insertBatch(List<TbBonusDetail> tbBonusDetails) {
        if(CollectionUtils.isEmpty(tbBonusDetails))return;
        tbBonusDetailMapper.insertBatch(tbBonusDetails);
    }

    @Override
    public Result<PageInfo<BonusDetailResultDto>> listPage(BonusCondDto bonusCondDto) {
        PageHelper.startPage(bonusCondDto.getPageNum(),bonusCondDto.getPageSize());
        Page<TbBonusDetail> tbBonusDetailPage=(Page<TbBonusDetail>)tbBonusDetailMapper.selectByCond(bonusCondDto);
        return convertToDto(tbBonusDetailPage);
    }

    private Result<PageInfo<BonusDetailResultDto>> convertToDto(Page<TbBonusDetail> tbBonusDetailPage) {
        Page<BonusDetailResultDto> bonusDetailResultDtoPage=new Page<BonusDetailResultDto>();
        BeanUtils.copyProperties(tbBonusDetailPage,bonusDetailResultDtoPage);
        if(tbBonusDetailPage.getResult()!=null){
            for(TbBonusDetail tbBonusDetail:tbBonusDetailPage.getResult()){
                BonusDetailResultDto bonusDetailResultDto=new BonusDetailResultDto();
                BeanUtils.copyProperties(tbBonusDetail,bonusDetailResultDto);
                bonusDetailResultDto.setSettleDate(DateUtil.getDateFormat(tbBonusDetail.getSettleDate()));
                bonusDetailResultDto.setUserLevelName(UserLevelEnum.getNameByCode(tbBonusDetail.getUserLevel()));
                bonusDetailResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbBonusDetail.getCreateTime()));
                bonusDetailResultDto.setModiTime(DateUtil.getDateTimeFormat(tbBonusDetail.getModiTime()));
                bonusDetailResultDtoPage.add(bonusDetailResultDto);
            }
        }
        return Result.newSuccess(new PageInfo<>(bonusDetailResultDtoPage));
    }

    @Override
    public List<BonusDetailResultDto> findOrderResultByCond(BonusCondDto bonusCondDto) {
        List<TbBonusDetail> tbBonusDetails=tbBonusDetailMapper.selectByCond(bonusCondDto);
        List<BonusDetailResultDto> bonusDetailResultDtos=new ArrayList<>();
        for(TbBonusDetail tbBonusDetail:tbBonusDetails){
            BonusDetailResultDto bonusDetailResultDto=new BonusDetailResultDto();
            BeanUtils.copyProperties(tbBonusDetail,bonusDetailResultDto);
            bonusDetailResultDto.setSettleDate(DateUtil.getDateFormat(tbBonusDetail.getSettleDate()));
            bonusDetailResultDto.setUserLevelName(UserLevelEnum.getNameByCode(tbBonusDetail.getUserLevel()));
            bonusDetailResultDto.setUserTypeName(UserTypeEnum.getNameByCode(tbBonusDetail.getUserType()));
            bonusDetailResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbBonusDetail.getCreateTime()));
            bonusDetailResultDto.setModiTime(DateUtil.getDateTimeFormat(tbBonusDetail.getModiTime()));
            bonusDetailResultDtos.add(bonusDetailResultDto);
        }
        return bonusDetailResultDtos;
    }

    @Override
    public List<TbSettleDetail> selectSettleGroup(Date genDate) {
        return tbBonusDetailMapper.selectSettleGroup(genDate);
    }
}
